package com.itsite.guider.demo.demo

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.itsite.guider.demo.R
import com.itsite.guider.demo.databinding.FragmentGuideBinding
import com.water.guider.Guide
import com.water.guider.Guider
import com.water.guider.JumpGravity
import com.water.guider.OnGuiderStopListener
import com.water.guider.highlight.DashCircleHighlight
import com.water.guider.highlight.DashRoundRectHighlight
import com.water.guider.highlight.Highlight
import com.water.guider.highlight.RoundRectHighlight
import com.water.guider.position.*

/**
 * 引导页在Fragment中使用的实例.
 */
class GuideFragment : Fragment(), View.OnClickListener {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = mBinding.root

    private val mBinding by lazy {
        FragmentGuideBinding.inflate(layoutInflater)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initGuider()
    }

    private fun initView() {
        mBinding.run {
            ivAdd.setOnClickListener(this@GuideFragment)
            ivAI.setOnClickListener(this@GuideFragment)
            ivShell.setOnClickListener(this@GuideFragment)
            ivBottle.setOnClickListener(this@GuideFragment)
            ivClockIn.setOnClickListener(this@GuideFragment)
        }
    }

    override fun onClick(v: View) {
        initGuider()
    }

    private fun initGuider() {
        val ringWidth = resources.getDimensionPixelSize(R.dimen.dp_10)
        val drinkAddGuide = Guide.Builder()
            .setPoint(mBinding.ivAdd)
            .setPosition(Position.top())
            .setHighlight(
                Highlight.dashCircle(ringWidth)
            )
            .setView(R.layout.guide_drink_add)
            .build()

        val aiGuide = Guide.Builder()
            .setPoint(mBinding.ivAI)
            .setPosition(CenterLeftBottomToBottom())
            .setHighlight(Highlight.dashCircle(ringWidth))
            .setView(R.layout.guide_drink_ai)
            .build()

        val shellGuide = Guide.Builder()
            .setPoint(mBinding.ivShell)
            .setPosition(CenterLeftBottom())
            .setHighlight(DashRoundRectHighlight(ringWidth))
            .setView(R.layout.guide_drink_shell)
            .build()

        val travelGuide = Guide.Builder()
            .setPoint(mBinding.ivBottle)
            .setPosition(RightCenterBottom())
            .setHighlight(DashCircleHighlight(ringWidth))
            .setView(R.layout.guide_drink_travel)
            .build()

        val clockInGuide = Guide.Builder()
            .setPoint(mBinding.ivClockIn)
            .setPosition(CenterLeftTop())
            .setHighlight(DashCircleHighlight(ringWidth))
            .setView(R.layout.guide_drink_clock_in)
            .build()

        val remindGuide = Guide.Builder()
            .setPoint(mBinding.ivRemind)
            .setPosition(Bottom())
            .setHighlight(DashRoundRectHighlight(ringWidth))
            .setView(R.layout.guide_drink_remind)
            .build()

        //shellGuide, travelGuide, clockInGuide

        Guider.Builder()
            .setAnchor(this)
            .addGuides(drinkAddGuide, remindGuide, aiGuide)
            .setMode(Guider.MODE_NEXT)
            .jumpGravity(JumpGravity.BOTTOM)
            .addOnGuiderStopListener(object : OnGuiderStopListener {
                override fun onStop() {
                    Log.i(TAG, "onStop: ")
                }
            }).show()
    }

    companion object {
        private const val TAG = "GuideFragment"
    }
}